package com.baosight.xCyber.model;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baosight.xCyber.TagInfo;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 节点基本属性
 *
 * @author 吴起航
 */

public class TagNode implements Serializable {
    public int id ;
    public String tagName = "";
    public transient String showName ="";
    private transient boolean itemFlag = true;
    public transient String remarkName;
    public transient String source;
    public transient String serviceId;
    public transient String unit;
    public String parentId="";
    public transient String treeName;
    public transient String desc;

    public int orderNo;

    public String getExtraParam() {
        return extraParam;
    }

    public void setExtraParam(String extraParam) {
        this.extraParam = extraParam;
    }

    public transient String extraParam;
    //设置不需要序列化的属性
    public transient TagNode otherTag;

    private transient boolean lazyLoad = false;
    private List<TagNode> items = new ArrayList<>();

    public boolean isItemFlag() {
        return itemFlag;
    }

    public String getTreeName() {
        return treeName;
    }

    public void setTreeName(String treeName) {
        this.treeName = treeName;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }


    public Map<String, String> getAttr() {
        return attr;
    }

    public void setAttr(Map<String, String> attr) {
        this.attr = attr;
    }

    private Map<String,String> attr=new HashMap<>();//其他属性


    public boolean isLazyLoad() {
        return lazyLoad;
    }


    public void setLazyLoad(boolean lazyLoad) {
        this.lazyLoad = lazyLoad;
    }


    public boolean getItemFlag() {
        return itemFlag;
    }


    public void setItemFlag(boolean itemFlag) {
        this.itemFlag = itemFlag;
    }


    public Integer getId() {
        return id;
    }


    public void setId(Integer id) {
        this.id = id;
    }


    public String getShowName() {
        return showName;
    }


    public void setShowName(String showName) {
        this.showName = showName;
    }


    public String getRemarkName() {
        return remarkName;
    }


    public void setRemarkName(String remarkName) {
        this.remarkName = remarkName;
    }


    public String getParentId() {
        return parentId;
    }


    public void setParentId(String parentId) {
        this.parentId = parentId;
    }


    public String getSource() {
        return source;
    }


    public void setSource(String source) {
        this.source = source;
    }


    public String getUnit() {
        return unit;
    }


    public void setUnit(String unit) {
        this.unit = unit;
    }


//	public Map<String, String> getAttr() {
//		return attr;
//	}
//
//
//	public void setAttr(Map<String, String> attr) {
//		this.attr = attr;
//	}


    public List<TagNode> getItems() {
        return items;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getTagName() {
        return tagName;
    }

    public void setTagName(String tagName) {
        this.tagName = tagName;
    }

    public String getServiceId() {
        return serviceId;
    }

    public void setServiceId(String serviceId) {
        this.serviceId = serviceId;
    }

    public void setItems(List<TagNode> items) {
        this.items = items;
    }


    public String itemsToString() {
        return JSON.toJSONString(items);
    }

    public int getOrderNo() {
        return orderNo;
    }

    public void setOrderNo(int orderNo) {
        this.orderNo = orderNo;
    }

    public TagNode(){}

    public TagNode(TagInfo tagInfo){
        this.id = tagInfo.id;
        this.tagName = tagInfo.tagName;
        this.showName = tagInfo.showName;
        this.remarkName = tagInfo.remarkName;
        this.source = tagInfo.source;
        this.parentId = tagInfo.parentId;
        this.serviceId = tagInfo.serviceId;
        this.unit = tagInfo.unit;
        this.treeName = tagInfo.treeName;
        this.desc = tagInfo.desc;

    }

    public TagNode(String treeName){
        this.treeName = treeName;
    }

//    public MainNode toMainNode(){
//        MainNode mainNode =new MainNode();
//        mainNode.id = this.tagName;
//        mainNode.parentId = this.parentId;
//        mainNode.showName = this.showName;
//        mainNode.remarkName = this.remarkName;
//        return mainNode;
//    }


    @Override
    public String toString() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("id", id);
        if(tagName.toLowerCase().contains("title")){
            jsonObject.put("tagName", showName);
        }else{
            jsonObject.put("tagName", tagName);
        }

        jsonObject.put("orderNo", orderNo);
        //jsonObject.put("treeName", treeName);
        //jsonObject.put("remarkName", remarkName);
        //jsonObject.put("showName", showName);
        //jsonObject.put("itemFlag", itemFlag);
        //jsonObject.put("source", source);
        //jsonObject.put("serviceId", serviceId);
        //jsonObject.put("parentId", parentId);
        //jsonObject.put("unit", unit);
        //jsonObject.put("lazyLoad", lazyLoad);

        if (items!=null&&items.size()!=0) {
            jsonObject.put("items", items.toArray());
        }

//        if(id!=0){
//            jsonObject.put("attr",attr);
//        }

//		Set<Entry<String,String>> set=attr.entrySet();
//		for(Entry<String,String> entry:set) {
//			jsonObject.put(entry.getKey(),entry.getValue());
//		}
        //jsonObject.put("items", items);
        return jsonObject.toString();
        //return this.toString(false);
    }

    public String toString(boolean itemFlag) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("id", id);
        jsonObject.put("tagName", tagName);
        jsonObject.put("remarkName", remarkName);
        jsonObject.put("showName", showName);
        jsonObject.put("source", source);
        jsonObject.put("serviceId", serviceId);
        jsonObject.put("parentId", parentId);
        jsonObject.put("unit", unit);
        jsonObject.put("lazyLoad", lazyLoad);
        jsonObject.put("itemFlag", itemFlag);
        if (itemFlag) {
            jsonObject.put("items", items.toArray());
        }

        return jsonObject.toString();
    }


    public static TagNode getTagNode(Map<String,Object> tagNodeMap){
        TagNode tagNode = new TagNode();
        for(Map.Entry<String,Object> entry:tagNodeMap.entrySet()){
            switch (entry.getKey()){
                case "ID":
                    tagNode.id = Integer.parseInt(entry.getValue().toString());
                    break;
                case "TAGNAME":
                    tagNode.tagName = entry.getValue().toString();
                    break;
                case "TREENAME":
                    tagNode.treeName = entry.getValue().toString();
                    break;
                case "SHOWNAME":
                    tagNode.showName = entry.getValue().toString();
                    break;
                case "REMARKNAME":
                    tagNode.remarkName = entry.getValue().toString();
                    break;
                case "PARENTNAME":
                    tagNode.parentId = entry.getValue().toString();
                    break;
                case "SOURCE":
                    tagNode.source = entry.getValue().toString();
                    break;
                case "SERVICEID":
                    tagNode.serviceId = entry.getValue().toString();
                    break;
                case "UNIT":
                    tagNode.unit = entry.getValue().toString();
                    break;
                case "ORDERNO":
                    tagNode.orderNo = (int)entry.getValue();
                    break;
                case "DESC":
                    tagNode.desc = entry.getValue().toString();
                    break;
                default:
                    tagNode.attr.put(entry.getKey(),entry.getValue().toString());
                    break;
            }
        }
        return tagNode;
    }
}
